﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>

    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0px; padding: 0px }
      #map_canvas { height: 100% }
    </style>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"> </script>
<script type="text/javascript">
var map;
var geocoder;
var startMarker;
var endMarker;   
function initialize() 
{     
        var latlng = new google.maps.LatLng(-34.397, 150.644);     
        var myOptions = {       
                zoom: 8,       
                center: latlng,       
                mapTypeId: google.maps.MapTypeId.ROADMAP     
                };     
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
        /*Don't need to initialize any marker
        var marker = new google.maps.Marker({position: latlng,
        draggable:true,
        animation : google.maps.Animation.DROP,
        title:"Hello World!"   });
        marker.setMap(map);*/
        //An example of an eventlistner
        //google.maps.event.addListener(marker, 'click', toggleBounce);
        startMarker = new google.maps.Marker({position: latlng});
        endMarker = new google.maps.Marker({position: latlng});
        
        geocoder = new google.maps.Geocoder(); 
}

function checkAddressNotNull(addrs) {
        if(addrs == "") {
                return false;
        }
        localAddr = addrs.replace(/^\s+¦\s+$/g, "");
        if(localAddr = "") {
                return false;
        }
        return true;
}

function codeAddress(whichAddr) {
        var address = document.getElementById(whichAddr).value;
        //alert("Address is "+address);
        //alert("whichAddr is "+whichAddr);
        
        if(checkAddressNotNull(address)) {
                geocoder.geocode( { 'address': address}, 
                function(results, status) 
                {       
                        if (status == google.maps.GeocoderStatus.OK) 
                        {         
                                map.setCenter(results[0].geometry.location);
                                //Check if startAddress
                                if(whichAddr == "startAddress") {
                                        startMarker.setMap(null);
                                        startMarker = new google.maps.Marker({map: map,position: results[0].geometry.location});
                                }
                                else {
                                        endMarker.setMap(null);
                                        endMarker = new google.maps.Marker({map: map,position: results[0].geometry.location});
                                }                       
                        } 
                        else 
                        {         
                                alert("Geocode was not successful for the following reason: " + status);       
                        }   
                }
                );
        }       
        else {
        //Clear appropriate markers if the address is blank.
                if(whichAddr == "startAddress") {
                        startMarker.setMap(null);
                }
                else{
                        endMarker.setMap(null);
                }
        }
}
//Associated method with the event listner
/*function toggleBounce() {
        if (marker.getAnimation() != null) 
        {     
                marker.setAnimation(null);   
        } 
        else 
        {     
                marker.setAnimation(google.maps.Animation.BOUNCE);   
        } 
}*/
</script> 

</head>
<body onload="initialize()">
<div id="map_canvas" style="width:400px; height:400px"></div>            
<div>
        <input id="startAddress" type="text" />
        <input type="button" value="StartAddress" onclick="codeAddress('startAddress')" /><br/>
        <input id="endAddress" type="text" />
        <input type="button" value="EndAddress" onclick="codeAddress('endAddress')"/><br/>
</div> 
<form id="form1" runat="server">
<div>
</div>
</form>
    
</body>
</html>
